`
https://leetcode.cn/problems/maximum-erasure-value/
`

/**
 * @param {number[]} nums
 * @return {number}
 */
var maximumUniqueSubarray = function (nums) {
  // 等价于找每个元素只能出现一次的最长子数组之和
  const n = nums.length
  const exist = new Map()
  let res = 0
  let sum = 0
  let left = 0, right = 0

  while (right < n) {
    const n1 = nums[right++]
    exist.set(n1, (exist.get(n1) || 0) + 1)
    sum += n1

    // 缩小窗口
    while (exist.get(n1) > 1) {
      const n2 = nums[left++]
      if (exist.get(n2) === 1) exist.delete(n2)
      else exist.set(n2, exist.get(n2) - 1)
      sum -= n2
    }

    // 更新答案
    res = Math.max(res, sum)
  }

  return res
};